﻿var dblclick = "click";
var starttime = 0;
var my_ad = "";
var my_new_url = "";
var select_id = 0;
var q_body = "";
var body1 = "";
var q_tit = "";
var q_url = "";
var q_id = 0;
var text_style_size = "";
var text_style_color = "";
var text_style_align = "";
var isedit = 0;

function ok8spost() {
    if ($("#q_tit").length > 0 && $("#q_okbtn").length > 0) {
        subques()
    } else {
        $("#ok8s_btn").remove();
        if ($(".c7").length > 0) {
            $(".c7").removeClass("c7 pa5")
        }
        if ($("#ok8sedit").length > 0) {
            $("#ok8sedit").remove()
        }
        if ($("#ok8sinput").length > 0) {
            $("#ok8sedit").remove()
        }
        q_tit = document.title;
        var body = $("body").html();
        var endnum = body.indexOf("main_ok8s_body_end");
        if (endnum < 0) {
            endnum = body.length
        }
        q_body = body.substring(body.indexOf("main_ok8s_body") + 16, endnum);
        body1 = $("#main_ok8s_body").length > 0 ? $("#main_ok8s_body").text() : $("body").text();
        body1 = body1.replace(q_tit, "");
        body1 = sub(body1, 90) + "...";
        var html = '<div class="tit1 h4"> </div><div class="cb n5"></div>' + '<div class="txt1">' + '<div class="txt1">标题：<br /><input type="text" id="q_tit" class="inp pa10" value="' + q_tit + '"/>' + '<div id="question" class="pa5"></div></div></div>' + '<div id="q_okbtn" class="k xn pa20 c1" data-h="subques()">确定提交</div>' + "</div>";
        showpop();
        $("#main").html(html)
    }
}
function ok8sdel() {
    p_obj.remove();
    if ($("#ok8sedit").length > 0) {
        $("#ok8sedit").remove()
    }
    if ($("#ok8sinput").length > 0) {
        $("#ok8sinput").remove()
    }
}
function setid(ddom) {
    $(ddom).find("*").each(function () {
        select_id++;
        $(this).attr("id", "a" + select_id)
    })
}
function readyok1() {
    var system = {
        win: false,
        mac: false,
        xll: false
    };
    var p = navigator.platform;
    system.win = p.indexOf("Win") == 0;
    system.mac = p.indexOf("Mac") == 0;
    system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
    if (system.win || system.mac || system.xll) {
        dblclick = "click";
        $(document).on("click", ".xn", function () {
            k_obj = $(this);
            eval(k_obj.attr("data-h"))
        })
    } else {
        dblclick = "touchend"
    }
    
    $(document).on("touchstart", "div,p,img,h1,h2,h3,h4,h5,h6,span,input,textarea,iframe,em,a", function () {
        k_obj = $(this);
        if (k_obj.attr("class") != null) {
            if (k_obj.attr("class").indexOf("xn") >= 0) {
                keydown = 1;
                k_obj.css("margin-top", "5px")
            } else {
                keydown = 2
            }
        } else {
            keydown = 2
        }
    });
    $(document).on("touchmove", "div,p,img,h1,h2,h3,h4,h5,h6,span,input,textarea,iframe,em,a", function () {
        if (keydown == 1 || keydown == 2) {
            keydown = 0;
            k_obj.css("margin-top", "0px")
        }
    });
    $(document).on(dblclick, "div,p,img,h1,h2,h3,h4,h5,h6,span,input,textarea,iframe,em,a", function () {
        if (keydown == 1) {
            keydown = 0;
            k_obj.css("margin-top", "0");
            eval(k_obj.attr("data-h"))
        } else {
            
            if (keydown == 2) {
                if ($("#mymodal").css("display") == "none") {
                    if (k_obj.attr("id").indexOf("a") != 0) {
                        //return
                    }
                    p_obj = k_obj;
                    showedit("a")
                }
            } else {
                if (dblclick == "click") {
                    k_obj = $(this);
                    if (k_obj.attr("id").indexOf("a") != 0) {
                        //return
                    }
                    p_obj = k_obj;
                    showedit("a")
                }
            }
        }
    })
}
function editcancel() {
    $("#ok8sedit").remove();
    $(".c7").removeClass("c7");
    $("#ok8sinput").remove()
}
function showmenu() {
    isedit = 1;
    var htm = '<div class="k xn c6 pa20 r33" data-h="editword()">插文字</div>	<div class="k xn c6 pa20 r33" data-h="ojax(\'/ask/advert/0\')">插广告</div>	<div class="k xn c9 pa20 r33" data-h="editcancel()">取消</div>';
    var utype = getkey("utype");
    if (utype && utype < 2) {
        htm += '<div class="k xn c6 pa20 r33" data-h="">会员才能插图片</div>'
    } else {
        if (typeof (yaoApplication) != "undefined") {
            htm += '<div class="k xn c6 pa20 r33" data-h="up_img(\'okimgadd\');">插图片</div>'
        } else {
            htm += '<div id="up_pic" class="k xn c6 pa20 r33" style="position:relative;text-align:center;"> 插图片<input type="file" id="file" style="width:100%;height:100%; cursor: pointer;outline: medium none; position: absolute; filter:alpha(opacity=0);-moz-opacity:0;opacity:0; left:0px;top: 0px;" onchange="showuphtml(1);"/></div>'
        }
    }
    htm += '<div class="k xn c6 pa20 r33"  data-h="upvideo()">插视频</div>';
    htm += '<div class="k xn c6 pa20 r33"  data-h="ojax(\'/ask/music/0\')">插音乐</div>';
    htm += '<div class="k xn c6 pa20 r33 cb" data-h="ok8sdel();">删除</div>' + '<div class="k xn c6 pa20 r33" data-h="delete_up_down(\'up\')">删前</div>' + '<div class="k xn c6 pa20 r33" data-h="delete_up_down(\'down\')">删后</div>';
    $("#ok8sedit").html(htm)
}
function editword() {
    var htm = '<div class="fr w50 c2 bn ma1 xn anc pa10 hh" data-h="editcancel();"><span class="icon-pause"></span><br>取消</div>';
    htm += '<div class="fl w50 c1 bn ma1 xn anc pa10 hh" data-h="$(\'#ok8stxt\').html(\'\');"><span class="icon-arrow-left"></span><br>清空</div>' + '<div class="cb n5"></div>' + '<div contenteditable="true" id="ok8stxt" class="inp hh3 pa2" style=" overflow:auto;"></div>' + '<div class="bn xn anc fl c2 w80 ma1 pa10 ra2 cb" data-h="charu()">添加</div>' + '<div class="bn xn anc fl c1 w80 ma1 pa10 ra2" data-h="more_style()">样式</div>' + '<div class="cb" id="face"></div>';
    $("#ok8sedit").html(htm)
}
function showedit(type) {
    tmp = type;
    if ($(".c7").length > 0) {
        $(".c7").removeClass("c7 pa5")
    }
    k_obj.addClass("c7 pa5");

    if ($("#ok8sedit").length > 0) {
        $("#ok8sedit").remove()
    }
    if ($("#ok8sinput").length > 0) {
        $("#ok8sinput").remove()
    }

    var htm = '<div id="ok8sedit" class="txt1 cb"  style="border: 2px solid #ff5876"></div>';
    text_style_size = "";
    text_style_color = "";
    text_style_align = "";
    k_obj.after(htm);
    showmenu()
}
$(document).ready(function () {
    readyok1();
    if ($("#bigger").length < 1) {
        select_id = 9999;
        $("#qid").after('<input id="bigger" type="hidden" value="' + select_id + '"/>')
    }
});

function ok8sok(i) {
    var txt = document.getElementById("ok8stxt");
    var o = document.createElement("IMG");
    o.src = http + "/ok/face/" + i + ".gif";
    txt.appendChild(o)
}
function charu() {
    select_id = $("#bigger").val();
    select_id++;
    $("#bigger").val(select_id);
    var txt = $("#ok8stxt").html();
    if ($(".c7").length > 0) {
        $(".c7").removeClass("c7 pa5")
    }
    $("#ok8sedit").remove();
    $("#ok8sinput").remove();
    var style = "";
    if (text_style_color != "") {
        style += "color:" + text_style_color + ";"
    }
    if (text_style_size != "") {
        style += "font-size:" + text_style_size + ";"
    }
    if (text_style_align != "") {
        style += "text-align:" + text_style_align + ";"
    }
    var html = '<div class="pa2" style="' + style + '" id="a' + select_id + '">' + txt + "</div>";
    p_obj.after(html)
}
function okimgadd(url) {
    select_id = $("#bigger").val();
    select_id++;
    $("#bigger").val(select_id);
    var htm = '<div><img id="a' + select_id + '" width="100%" src="' + upurl + url + '"></div>';
    p_obj.after(htm);
    if ($(".c7").length > 0) {
        $(".c7").removeClass("c7 pa5")
    }
    $("#ok8sedit").remove();
    $("#ok8sinput").remove()
}
function oknetimgadd(url) {
    select_id = $("#bigger").val();
    select_id++;
    $("#bigger").val(select_id);
    var htm = '<div><img  id="a' + select_id + '" src="' + url + '"></div>';
    p_obj.after(htm);
    showmodal(0);
    if ($(".c7").length > 0) {
        $(".c7").removeClass("c7 pa5")
    }
    $("#ok8sedit").remove();
    $("#ok8sinput").remove()
}
function quit_add(obj) {
    $("#ok8stxt").val("");
    $("#ok8surl").val("");
    removekey("myurl")
}
function subques0(id, y, m, d, uid, url) {
    var k = getkey("k");
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        k = k + "%2F2"
    } else {
        k = k + "%2F3"
    }
    var data = {
        id: id,
        y: y,
        m: m,
        d: d,
        tit: $("#q_tit").val(),
        body: q_body
    };
    $.ajax({
        url: "http://www.ok8s.com:83/htmlpost?k=" + k,
        data: data,
        type: "POST",
        dataType: "json",
        complete: function (xhr, result) {
            if (!xhr) {
                alert("网络连接失败,上传错误。");
                return
            }
            var text = xhr.responseText;
            if (!text) {
                alert("网络错误,上传错误。");
                return
            }
            var arr = text.split("{}");
            if (arr[0] == "ok") {
                loadfile("http://www.ok8s.com:83" + arr[1] + "?u=" + uid + "&s=" + url)
            } else {
                alert("错误，请重试。" + arr[1])
            }
        }
    })
}
function subques() {
    var f = 1;
    var x = "";
    var tit = $("#q_tit").val();
    var t = tit + "[]" + q_body + "[]" + q_url + "[]" + q_id + "[]";
    var k = $("#q_type").val();
    $("#question input").each(function (i) {
        x = trim($(this).val());
        if (x.length > 0) {
            t += x + "[]"
        } else {
            f = 0;
            alert("请填写完整");
            return false
        }
    });
    if (f == 1) {
        t = t.replace(/&/gi, "@8@");
        ojax("/ask/edit/" + k, t)
    }
}
function set_edit_face() {
    var htm = "";
    for (var i = 0; i < 135; i++) {
        htm += '<img class="k xn w50 fl" data-h="ok8sok(' + i + ')" src="../face/' + i + '.gif" />'
    }
    htm = '<div class="cb n5"></div><div class="k xn fr c5" data-h="ojax(\'/ask/netimg/0_0\')">动漫表情</div><div class="cb n5"></div><div> ' + htm + "</div>";
    $("#face").html(htm)
}
function add_my_ad(id) {
    var ad = $("#insertad_" + id).html();
    showmodal(0);
    if (ad.length < 1) {
        alert("你还木有编辑你的连接，请在‘我的’里面添加文字广告。");
        return
    } else {
        if ($(".c7").length > 0) {
            $(".c7").removeClass("c7 pa5")
        }
        $("#ok8sedit").remove();
        $("#ok8sinput").remove();
        select_id = $("#bigger").val();
        select_id++;
        $("#bigger").val(select_id);
        p_obj.after('<div id="a' + select_id + '">' + ad + "</div>")
    }
}
function delete_up_down(type) {
    if (type == "up") {
        var upbr = p_obj.prev();
        var upbd;
        while (upbr.length > 0) {
            upbd = upbr.prev();
            upbr.remove();
            upbr = upbd
        }
        var uppa = p_obj.parent();
        while (uppa.length > 0) {
            upbr = uppa.prev();
            while (upbr.length > 0) {
                upbd = upbr.prev();
                upbr.remove();
                upbr = upbd
            }
            if (uppa.attr("id") == "main_ok8s_body") {
                break
            }
            if (uppa.attr("id") == "main") {
                $("#main").html('<div id="main_ok8s_body">' + $("#main").html());
                $("#main").before('<div id="mymodal"></div>');
                break
            }
            uppa = uppa.parent();
        }
    } else {
        var dnbr = p_obj.next();
        var dnbd;
        while (dnbr.length > 0) {
            dnbd = dnbr.next();
            dnbr.remove();
            dnbr = dnbd
        }
        var uppa = p_obj.parent();
        while (uppa.length > 0) {
            dnbr = uppa.next();
            while (dnbr.length > 0) {
                dnbd = dnbr.next();
                dnbr.remove();
                dnbr = dnbd
            }
            uppa = uppa.parent()
        }
    }
    if ($(".c7").length > 0) {
        $(".c7").removeClass("c7 pa5")
    }
    $("#ok8sedit").remove();
    $("#ok8sinput").remove()
}
function more_style() {
    var size = "";
    var color = "";
    for (var i = 0; i++; i <= 17) {
        color += ""
    }
    var htm = '<div class="cb n5"></div>';
    htm += '<div class="txt1 pa20">                <div class="txt1 c8 ra3 pa1"><div class="c3 ra3 pa2">' + '<div class="pa10 cb liti">字体大小</div>' + '<div class="k xn c4 r33 hh pa2" style="font-size:24px" data-h="text_style(\'text_style_size\',\'24\')">最大</div>' + '<div class="k xn c4 r33 hh" style="font-size:18px" data-h="text_style(\'text_style_size\',\'18\')">一般</div>' + '<div class="k xn c4 r33 hh" style="font-size:14px" data-h="text_style(\'text_style_size\',\'14\')">最小</div>' + '<div class="cb n5"></div></div></div>                <div class="cb n5"></div>             </div>';
    htm += '<div class="txt1 pa20">                <div class="txt1 c8 ra3 pa1"><div class="c3 ra3 pa2">' + '<div class="pa10 cb liti">字体颜色</div>' + '<div class="k xn r20" style="background-color: #000000" data-h="text_style(\'text_style_color\',\'#000000\')">&nbsp;</div>' + '<div class="k xn r20" style="background-color: #FF0000" data-h="text_style(\'text_style_color\',\'#FF0000\')">&nbsp;</div>' + '<div class="k xn r20" style="background-color: #FF00FF" data-h="text_style(\'text_style_color\',\'#FF00FF\')">&nbsp;</div>' + '<div class="k xn r20" style="background-color: #5B00AE" data-h="text_style(\'text_style_color\',\'#5B00AE\')">&nbsp;</div>' + '<div class="k xn r20" style="background-color: #0000C6" data-h="text_style(\'text_style_color\',\'#0000C6\')">&nbsp;</div>' + '<div class="k xn r20" style="background-color: #00CACA" data-h="text_style(\'text_style_color\',\'#00CACA\')">&nbsp;</div>' + '<div class="k xn r20" style="background-color: #00DB00" data-h="text_style(\'text_style_color\',\'#00DB00\')">&nbsp;</div>' + '<div class="k xn r20" style="background-color: #F9F900" data-h="text_style(\'text_style_color\',\'#F9F900\')">&nbsp;</div>' + '<div class="k xn r20" style="background-color: #984B4B" data-h="text_style(\'text_style_color\',\'#984B4B\')">&nbsp;</div>' + '<div class="k xn r20" style="background-color: #9F4D95" data-h="text_style(\'text_style_color\',\'#9F4D95\')">&nbsp;</div>' + '<div class="cb n5"></div></div></div>                <div class="cb n5"></div>             </div>';
    htm += '<div class="txt1 pa20">                <div class="txt1 c8 ra3 pa1"><div class="c3 ra3 pa2">' + '<div class="pa10 cb liti">对齐方式</div>' + "<div class=\"k xn c4 anl r33\"  data-h=\"text_style('text_style_align','left')\">居左对齐</div>" + "<div class=\"k xn c4 anc r33\" data-h=\"text_style('text_style_align','center')\">居中对齐</div>" + "<div class=\"k xn c4 anr r33\" data-h=\"text_style('text_style_align','right')\">居右对齐</div>" + '<div class="cb n5"></div></div></div>                <div class="cb n5"></div><div class="k xn c2 pa5 ra5 ma1 w120" data-h="showmodal(0)">确定</div>             </div>';
    $("#mymodal").html(htm);
    showmodal(1)
}
function changer_area_bydom(dom) {
    var st = $(dom).css("display");
    if (st == "block") {
        $(dom).css("display", "none")
    } else {
        $(dom).css("display", "block")
    }
}
function text_style(name, val) {
    k_obj.siblings(".k").css("border-color", "rgb(252, 248, 248)");
    k_obj.css("border-color", "rgb(192, 56, 56)");
    switch (name) {
        case "text_style_color":
            text_style_color = val;
            $("#ok8stxt").css("color", val);
            break;
        case "text_style_size":
            text_style_size = val;
            $("#ok8stxt").css("font-size", val);
            break;
        case "text_style_align":
            text_style_align = val;
            $("#ok8stxt").css("text_align", val);
            break
    }
}
function toad(x) {
    if ($("#mymodal").css("display") == "none") {
        k_obj = k_obj.parent();
        p_obj = k_obj;
        showedit("a")
    }
}
function upvideo() {
    var html = '<div class="txt1"><div class="tit1 h4">插入视频</div>  <div class="xn k c1 fr" data-h="showmodal(0)">返回</div> <div class="cb n5"></div>' + '<div class="txt1"><input id="videourl" type="text" class="inp pa10"/></div>' + '<div class="k w100 fr c6 xn" data-h="alert(\'请长按粘贴。\')">粘贴</div>' + '<div class="xn k c2 w100 fr" data-h="intovideo()">插入</div> </div>' + '<div class="txt1">请插入优酷，土豆，腾讯等网站的通用代码或者html代码。<hr>步骤：<br>1、在电脑上打开优酷视频，点开分享右边的箭头，复制通用代码<br>2、把代码发到手机上，比如发到手机QQ，再到手机上复制代码，粘贴到此处<br><hr>即将推出的企业版可以直接在电脑上操作</div>';
    $("#mymodal").html(html);
    showmodal(1)
}
function intovideo() {
    select_id = $("#bigger").val();
    select_id++;
    $("#bigger").val(select_id);
    var url = $("#videourl").val();
    if (url.indexOf("http") == 0) {
        url = '<embed src="' + url + '" allowFullScreen="true" quality="high" width="100%" height="300" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"></embed>'
    }
    var html = '<div id="a' + select_id + '" class="txt1 pa20">' + url + "</div>";
    p_obj.after(html);

    if ($(".c7").length > 0) {
        $(".c7").removeClass("c7 pa5")
    }
    $("#ok8sedit").remove();
    $("#ok8sinput").remove();
    showmodal(0)
};